package com.blockmeta.bbs.immodule.widget;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.blockmeta.bbs.baselibrary.widget.popupview.BaseBottomPopupWindow;
import com.blockmeta.bbs.businesslibrary.net.pojo.BaseImResponse;
import com.blockmeta.bbs.businesslibrary.net.retrofit.RetrofitFactory;
import com.blockmeta.bbs.businesslibrary.net.rxjava.CommonSubscriber;
import com.blockmeta.bbs.businesslibrary.net.rxjava.RxUtil;
import com.blockmeta.bbs.businesslibrary.util.AccountHelper;
import com.blockmeta.bbs.businesslibrary.widget.RoundCornerTextView;
import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.bean.beaninterface.IJoinCircleRequestEntry;
import com.blockmeta.bbs.immodule.corebean.IMMetaBase;
import com.blockmeta.bbs.immodule.corebean.Impl.IMApplicationContext;
import com.blockmeta.bbs.immodule.net.api.DealWithTodoApi;


public class Popup_Circle_Add_Request extends BaseBottomPopupWindow implements View.OnClickListener {

    RoundCornerTextView mBtnConfirm;
    RoundCornerTextView mBtnCancel;
    LengthLimitEditText mEdtReason;
    TextView mTvTitle;
    TextView mTvContent;
    IJoinCircleRequestEntry mTodo;


    public Popup_Circle_Add_Request(Context context, IJoinCircleRequestEntry todo) {
        super(context);
        mTodo = todo;
        bindTodo(todo);

    }

    private void bindTodo(IJoinCircleRequestEntry todo) {
        mTvContent.setText(todo.getRequestMsg());
    }


    @Override
    public int getResId() {
        return R.layout.im_popup_two_button;
    }

    @Override
    protected void bindEvent() {

        mBtnConfirm = (RoundCornerTextView) findViewById(R.id.im_popup_sys_btn_confirm);
        mBtnCancel = (RoundCornerTextView) findViewById(R.id.im_popup_sys_btn_cancel);
        mBtnConfirm.setOnClickListener(this);
        mBtnCancel.setOnClickListener(this);
        mTvContent = (TextView) findViewById(R.id.im_popup_sys_msg);
        mTvTitle = (TextView) findViewById(R.id.im_popup_sys_tv_title);
        mEdtReason = (LengthLimitEditText) findViewById(R.id.edt_back);

    }

    @Override
    public View getClickToDismissView() {
        return popupView.findViewById(R.id.rlayout_notice_pop_container);
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.im_popup_sys_btn_confirm) {
            dealWithTodo(mTodo.getTodoId(),true);


        } else if (v.getId() == R.id.im_popup_sys_btn_cancel) {
            dealWithTodoAndDelete(mTodo);
        }
    }

    private void dealWithTodoAndDelete(final IJoinCircleRequestEntry todo) {
        if(!todo.isCompleted()){
            RetrofitFactory.getRetrofit().create(DealWithTodoApi.class)
                    .dealWithTodo(AccountHelper.getUidInt(),todo.getTodoId(), false, mEdtReason.getText().toString())
                    .compose(RxUtil.dataNullResult())
                    .compose(RxUtil.<BaseImResponse<Object>>rxSchedulerHelper())
                    .subscribe(new CommonSubscriber<BaseImResponse<Object>>(getContext()) {
                        @Override
                        public void onNext(BaseImResponse<Object> o) {
                            IMApplicationContext.getTodoDomainService().deleteTodo(todo.getTodoId());
                            dismiss();
                        }
                    });

        }else {
            IMApplicationContext.getTodoDomainService().deleteTodo(todo.getTodoId());
            dismiss();
        }
    }

    /**
     * 处理代办
     */
    protected void dealWithTodo(final long todoId, final boolean agreeOrNot){
        RetrofitFactory.getRetrofit().create(DealWithTodoApi.class)
                .dealWithTodo(AccountHelper.getUidInt(),todoId,agreeOrNot, mEdtReason.getText().toString())
                .compose(RxUtil.<BaseImResponse<Object>>rxSchedulerHelper())
                .compose(RxUtil.commonResult())
                .subscribe(new CommonSubscriber<Object>(getContext()) {
                    @Override
                    public void onNext(Object o) {
                        IMApplicationContext.getTodoDomainService().updateTodo(todoId,true,agreeOrNot,"");
                        if(agreeOrNot && mTodo.getGroupId() != 0){
                            IMApplicationContext.getIMMetaService().post(new IMMetaBase.Event_AddMemberToCircle(mTodo.getGroupId()));
                        }
                        dismiss();
                    }
                });
    }

    public void showPop() {
        if (!isShowing()) {
            showPopupWindow();
        }
    }

    @Override
    public View initAnimaView() {
        return null;
    }
}
